package com.hehe.ai.langchain4j.config;

import com.hehe.ai.langchain4j.store.MongoChatMemoryStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.apache.tika.ApacheTikaDocumentParser;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.file.FileSystems;
import java.nio.file.PathMatcher;
import java.util.Arrays;
import java.util.List;

/**
 * @author: hehe
 * @create: 2025-05-14 15:02
 * @Description:
 */
@Configuration
@RequiredArgsConstructor
public class HeHeAgentConfig {

    private final MongoChatMemoryStore mongoChatMemoryStore;

    private final EmbeddingModel embeddingModel;
    private final EmbeddingStore embeddingStore;
    @Bean
    public ChatMemoryProvider chatMemoryProviderHehe() {
        return memoryId->MessageWindowChatMemory.builder()
                    .id(memoryId)
                    .maxMessages(20)
                    .chatMemoryStore(mongoChatMemoryStore)
                    .build();
    }

    @Bean
    ContentRetriever contentRetrieverHehe() {
        // 创建一个 EmbeddingStoreContentRetriever 对象，用于从嵌入存储中检索内容
        return EmbeddingStoreContentRetriever
                .builder()
                .embeddingModel(embeddingModel) // 设置用于生成嵌入向量的嵌入模型
                .embeddingStore(embeddingStore) // 指定要使用的嵌入存储
                .maxResults(1) // 设置最大检索结果数量，这里表示最多返回 1 条匹配结果
                .minScore(0.8) // 设置最小得分阈值，只有得分大于等于 0.8 的结果才会被返回
                .build(); // 构建最终的 EmbeddingStoreContentRetriever 实例
    }

    /*@Bean
    ContentRetriever contentRetrieverHehe() {
        //使用FileSystemDocumentLoader读取指定目录下的知识库文档
        //并使用默认的文档解析器对文档进行解析
        PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:*.md");
        List<Document> documents = FileSystemDocumentLoader.loadDocuments("src/main/resources/knowledge",
                pathMatcher, new ApacheTikaDocumentParser());
        *//*Document document1 = FileSystemDocumentLoader.loadDocument("src/main/resources/knowledge/医院信息.md",new ApacheTikaDocumentParser());
        Document document2 = FileSystemDocumentLoader.loadDocument("src/main/resources/knowledge/科室信息.md",new ApacheTikaDocumentParser());
        Document document3 = FileSystemDocumentLoader.loadDocument("src/main/resources/knowledge/神经内科.md",new ApacheTikaDocumentParser());
        List<Document> documents = Arrays.asList(document1, document2, document3);*//*

        //使用内存向量存储
        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
        //使用默认的文档分割器
        EmbeddingStoreIngestor.ingest(documents, embeddingStore);
        //从嵌入存储（EmbeddingStore）里检索和查询内容相关的信息
        return EmbeddingStoreContentRetriever.from(embeddingStore);
    }*/
}
